package cn.iocoder.yudao.module.enterprise.service.datamanage;

import cn.iocoder.yudao.module.carbon.api.accountboundary.AccountBoundaryApi;
import cn.iocoder.yudao.module.carbon.api.accountboundary.dto.AccountBoundaryListReqDTO;
import cn.iocoder.yudao.module.carbon.api.accountboundary.dto.AccountBoundaryRespDTO;
import cn.iocoder.yudao.module.enterprise.controller.admin.datamanage.vo.DataManageCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.datamanage.vo.DataManageListReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.datamanage.vo.DataManageListRespVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.datamanage.vo.DataManageUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.datamanage.DataManageConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.datamanage.DataManageDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.datamanage.DataManageMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.DATA_MANAGE_NOT_EXISTS;

/**
 * 数据管理计划 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class DataManageServiceImpl implements DataManageService {

    @Resource
    private DataManageMapper dataManageMapper;

    @Resource
    private AccountBoundaryApi accountBoundaryApi;

    @Override
    public Long createDataManage(DataManageCreateReqVO createReqVO) {
        // 插入
        DataManageDO dataManage = DataManageConvert.INSTANCE.convert(createReqVO);
        dataManageMapper.insert(dataManage);
        // 返回
        return dataManage.getId();
    }

    @Override
    public void updateDataManage(DataManageUpdateReqVO updateReqVO) {
        // 校验存在
        validateDataManageExists(updateReqVO.getId());
        // 更新
        DataManageDO updateObj = DataManageConvert.INSTANCE.convert(updateReqVO);
        dataManageMapper.updateById(updateObj);
    }

    @Override
    public void deleteDataManage(Long id) {
        // 校验存在
        validateDataManageExists(id);
        // 删除
        dataManageMapper.deleteById(id);
    }

    private void validateDataManageExists(Long id) {
        if (dataManageMapper.selectById(id) == null) {
            throw exception(DATA_MANAGE_NOT_EXISTS);
        }
    }

    @Override
    public DataManageDO getDataManage(Long id) {
        return dataManageMapper.selectById(id);
    }

    @Override
    public List<DataManageListRespVO> getDataManageList(DataManageListReqVO listReqVO) {
        List<AccountBoundaryRespDTO> accountBoundaryList = accountBoundaryApi.getAccountBoundaryList(
                new AccountBoundaryListReqDTO().setStandard(listReqVO.getStandard())).getCheckedData();
        List<DataManageDO> dataManageList = dataManageMapper.selectListByPlanId(listReqVO.getPlanId());

        List<DataManageListRespVO> respList = DataManageConvert.INSTANCE.convertList(accountBoundaryList);
        for (DataManageDO dataManageDO : dataManageList) {
            Long accountBoundaryId = dataManageDO.getAccountBoundaryId();
            respList.forEach(respVO -> {
                if (respVO.getId().equals(accountBoundaryId)) {
                    respVO.setDataManageId(dataManageDO.getId()).setDataDesc(dataManageDO.getDataDesc());
                }
            });
        }

        return respList;
    }
}
